package com.fone.player.service;

import java.util.Random;

import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.Context;
import android.content.Intent;
import android.os.Build;
import android.support.v4.app.NotificationCompat;
import android.support.v4.app.NotificationCompat.Builder;
import android.widget.RemoteViews;

import com.fone.player.R;
import com.fone.player.entity.ContentSubscribe;
import com.fone.player.play.FonePlayerActivity;
import com.fone.player.play.PlayerAction;
import com.fone.player.play.PlayerFrom;
import com.fone.player.storage.StorageModule;
import com.fone.player.util.L;

/**
 * 处理节目单预约
 * 
 * @author kongxiaojun
 * @since 2014-7-2
 */
public class LiveCardHandler extends Thread {

	private boolean isRunning = false;

	private static final String TAG = "LiveCardHandler";

	private Context mContext;

	public LiveCardHandler(Context mContext) {
		super();
		this.mContext = mContext;
	}

	@Override
	public void run() {
		isRunning = true;
		while (true) {
			try {
				// 获取离当前时间最近的节目单
				ContentSubscribe sub = StorageModule.getInstance().getNearestContentSubscribe();
				if (sub != null) {
					long currentTime = System.currentTimeMillis();
					long plus = currentTime - sub.getContentSubscribeTime();
					if (plus >= 0 && plus < 60000) {
						// 1分钟以内的就进行提示
						sendLiveCardNotification(sub);
					} else if (plus > 60000) {
						// 当前分钟内没有就睡眠30秒
						Thread.sleep(30000);
					}
				} else {
					// 没有预约睡眠30秒
					Thread.sleep(30000);
				}
				Thread.sleep(3000);
			} catch (InterruptedException e) {
				e.printStackTrace();
			}
		}
	}

	private void sendLiveCardNotification(ContentSubscribe sub) {
		RemoteViews mRemoveViews = new RemoteViews(mContext.getPackageName(), R.layout.outside_notification_view);
		mRemoveViews.setTextViewText(R.id.notification_title_tv, sub.getContentSubscribeName());
		mRemoveViews.setTextViewText(R.id.notification_content_tv, String.format(mContext.getText(R.string.video_order_card_notify_text).toString(), sub.getContentSubscribeName()));

		Intent notificationIntent = new Intent();
		notificationIntent.putExtra("name", sub.getContentSubscribeName());
		notificationIntent.putExtra("vgdetail", sub.getContentSubscribeUrl());
		notificationIntent.putExtra("from", PlayerFrom.NOTIFY_SUBSCRIBE);
		notificationIntent.putExtra("action", PlayerAction.DETAIL.name());
		notificationIntent.setAction(NotificationReceiver.NOTIFICATION_BOOK);
		notificationIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);

		Random r = new Random();
		int tmpID = r.nextInt(1000000000);
		L.i(TAG, "sendNotification", "" + tmpID);
		PendingIntent contentItent = PendingIntent.getBroadcast(mContext, tmpID, notificationIntent, PendingIntent.FLAG_UPDATE_CURRENT);
		NotificationManager nm = (NotificationManager) mContext.getSystemService(Context.NOTIFICATION_SERVICE);
		if (android.os.Build.VERSION.SDK_INT <= 10) {
			// 针对3.0以下版本通知
			@SuppressWarnings("deprecation")
			Notification notification = new Notification(R.drawable.icon, String.format(mContext.getText(R.string.video_order_card_notify_text).toString(), sub.getContentSubscribeName()), System.currentTimeMillis());
			notification.flags |= Notification.FLAG_AUTO_CANCEL; // 点击通知后取消
			notification.defaults |= Notification.DEFAULT_VIBRATE;// 震动提示
			notification.defaults = Notification.DEFAULT_SOUND | Notification.DEFAULT_LIGHTS; // 声音,Led提示
			notification.contentIntent = contentItent;
			notification.contentView = mRemoveViews;
			nm.notify(sub.getContentSubscribeId(), notification);
		} else {
			// 针对3.0以上的通知
			NotificationCompat.Builder mBuilder = new Builder(mContext);
			mBuilder.setSmallIcon(R.drawable.icon);
			mBuilder.setContentText(String.format(mContext.getText(R.string.video_order_card_notify_text).toString(), sub.getContentSubscribeName()));
			mBuilder.setDefaults(Notification.DEFAULT_SOUND);
			mBuilder.setAutoCancel(true);
			mBuilder.setContentIntent(contentItent);
			mBuilder.setContent(mRemoveViews);
			nm.notify(sub.getContentSubscribeId(), mBuilder.build());
		}

		// 根据id删除数据库中对应条目
		StorageModule.getInstance().deleteContentSubscribeById(sub.getContentSubscribeId());
		L.v(TAG, "节目单提醒完成" + System.currentTimeMillis());
	}

	@Override
	public synchronized void start() {
		if (isRunning) {
			return;
		}
		super.start();
	}

}
